package com.sunday.shoufen.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.common.utils.DeviceUtils;
import com.sunday.shoufen.R;
import com.sunday.shoufen.entity.IndexClassify;
import com.sunday.shoufen.ui.MainActivity;
import com.sunday.shoufen.ui.ShopListActivity;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘涛 on 2017/10/25.
 */

public class IndexClassifyAdapter extends RecyclerView.Adapter {



    private List<IndexClassify> dataSet;
    private Context mContext;


    public IndexClassifyAdapter(Context context) {
        this.mContext = context;
    }

    public IndexClassifyAdapter(Context context, List<IndexClassify> datas) {
        this.dataSet = datas;
        this.mContext = context;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_index_classify, null);
        RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(DeviceUtils.getDisplay(mContext).widthPixels / 4, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(layoutParams);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder listHolder= (ViewHolder) holder;
        IndexClassify item=dataSet.get(position);
        Glide.with(mContext)
                .load(item.getImage())
                .error(R.mipmap.ic_default)
                .into(listHolder.img);
        listHolder.name.setText(item.getName());
        listHolder.linearLayout.setTag(item);
    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }

    public void setDataSet(List<IndexClassify> dataSet) {
        this.dataSet = dataSet;
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @Bind(R.id.img)
        ImageView img;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.ll_index_cat)
        LinearLayout linearLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            linearLayout.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            IndexClassify item= (IndexClassify) v.getTag();
            Intent intent=new Intent(mContext, ShopListActivity.class);
            intent.putExtra("item",item);
            mContext.startActivity(intent);
        }
    }
}
